package com.itheima;

import org.junit.jupiter.api.Test;

import java.util.*;
import java.util.stream.Collectors;

public class CollectionTest {
    public static void main(String[] args) {
        HashSet<String> set = new HashSet<>();
        set.add("a");
        set.add("b");
        set.add("c");
        set.add("d");
        System.out.println(set);
    }

    @Test
    public void testArrayList() {
        ArrayList<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add(2,"中间插入");
//        list.clear();
        System.out.println(list);
        System.out.println(list.isEmpty());
        System.out.println(list.size());
        System.out.println(list.contains("a"));
        list.remove("a");
        System.out.println(list.get(0));
        Object[] arr = list.toArray();
        System.out.println(Arrays.toString(arr));


        ArrayList<String> list2 = new ArrayList<>();
        list2.add("e");
        list.addAll(list2);
        System.out.println(list);

        // 迭代器遍历
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }

        // for 遍历集合或数组
        for(String e:list) {
            System.out.println(e);
        }
        System.out.println("------------");
        // forEach
        list.forEach(s->System.out.println(s));
    }

    @Test
    public void testLinkedList() {
        LinkedList<String> queue = new LinkedList<>();
        queue.addLast("a");
        queue.addFirst("b");
        queue.addFirst("c");
        queue.addFirst("d");
        queue.removeFirst();
        System.out.println(queue);
    }

    @Test
    public void testHashSet() {
        Set<Integer> set = new HashSet<>();
        set.add(1);
        set.add(2);
        set.add(3);
        set.add(5);
        set.add(4);
        set.add(7);
        System.out.println(set);
    }

    @Test
    public void testHashMap() {
        Map<String, String> map = new HashMap<>();
        map.put("a", "a");   // pullAll
        map.put("b", "b1");
        System.out.println(map);
        System.out.println(map.size());
        System.out.println(map.isEmpty());
        System.out.println(map.get("a"));
        System.out.println(map.containsKey("b"));
        System.out.println(map.containsValue("b"));

        Set<String> set = map.keySet();
        System.out.println(set);
        Collection<String> values = map.values(); // 获取所有值
        System.out.println(values);
    }

    @Test
    public void testStream() {
        List<String> list = Arrays.asList("a", "b", "c", "d");
        System.out.println(list);
        List<String> a = list.stream().filter(s -> s.startsWith("a")).collect(Collectors.toList());
        System.out.println(a);
        // 中间方法 --------------------
        // sorted 排序
        // limit 前几位
        // skip 跳过几个
        // distinct 去重
        // map
        // concat 合并

        // 终结方法 --------------------
        // forEach   count    max     min     collect    toArray

    }
}
